package main.java.com.fxy.controller;

import main.java.com.fxy.pojo.User;
import main.java.com.fxy.service.UserService;
import main.java.com.fxy.service.impl.UserServiceImpl;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.*;

@WebServlet("/UserController")
public class UserController extends HttpServlet {
    private static final long serialVersionUID = 1L;
    private UserService userService = new UserServiceImpl();

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String action = request.getParameter("action");
        System.out.println("action=" + action);
        if("login".equals(action)) {
            login(request, response);
        } else if ("regist".equals(action)) {
            regist(request, response);
        }
    }

    protected void regist(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter out = resp.getWriter();
        User user =new User();
        user.setUsername(req.getParameter("userName"));
        user.setPassword(req.getParameter("userPwd"));
        user.setEmail(req.getParameter("email"));
        boolean isSuccess = userService.regist(user);
        if(isSuccess){
            req.setAttribute("message", "注册成功!请登录");
            req.getRequestDispatcher("login.jsp").forward(req, resp);
        }else{
            req.setAttribute("message", "用户名已存在，注册失败!请重新注册");
            req.getRequestDispatcher("register.jsp").forward(req, resp);
        }
    }


    protected void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter out = resp.getWriter();
        User user =new User();
        user.setUsername(req.getParameter("userName"));
        user.setPassword(req.getParameter("userPwd"));
        String checkcode = req.getParameter("checkCode");
        HttpSession session = req.getSession();
        session.setAttribute("user", user.getUsername());
        String checkCodeServlettext = (String) session.getAttribute("checkCodeServlettext");
        checkcode=checkcode.toLowerCase();
        checkCodeServlettext=checkCodeServlettext.toLowerCase();
        boolean isSuccess = userService.login(user);
        if(checkcode!=null&&checkcode.equals(checkCodeServlettext))
        {
            System.out.println(checkcode+ " " +checkCodeServlettext);
            if(isSuccess){
                System.out.println("success");
                req.setAttribute("message", "登录成功!");
                req.getRequestDispatcher("firstpage.jsp").forward(req, resp);
            }else{
                System.out.println("fail");
                req.setAttribute("message", "登录失败!请重新登录");
                req.getRequestDispatcher("login.jsp").forward(req, resp);
            }
        }else{
            System.out.println(checkcode+ " " +checkCodeServlettext);
            System.out.println("fail");
            req.setAttribute("message", "验证码错误!请重新登录");
            req.getRequestDispatcher("login.jsp").forward(req, resp);
        }
    }
}
